#!/usr/bin/env python
#-------------------------------------------------------------------------------
# Copyright (c) 2008 Andrew Coleman under the MIT License
# See COPYING at the root directory of this project.
#-------------------------------------------------------------------------------
# Standard Modules
import optparse


def ParseArgs():
    '''Parse command line arguments'''
    parser = optparse.OptionParser(version="%prog 0.01")
    parser.add_option("-v", "--verbose", dest="verbosity", action="store_true", help="Explicit debug messages. (equivalent to -m debug)")
    parser.add_option("-q", "--quiet", dest="verbosity", action="store_false", help="Silent execution, no output (equivalent to -m quiet)")
    parser.add_option("-m", "--mode", dest="mode", help="Set the level of output (default: %default)\nThe possible levels, "
    "from most silent to most verbose are: 'quiet', 'critical', 'error', 'warning', 'info', and 'debug'. The level selected means that it "
    "and all less verbose levels will be output, excluding the special case of 'quiet'. -m error would result in error and critical "
    "messages displaying. -m quiet would result in no output, (equivalent to using -q). -m debug would result in all possible output "
    "(equivalent to using -v)")

    group = optparse.OptionGroup(parser, "IRC Options", "These options specify IRC behavior.")
    group.add_option("-s", "--server", dest="server", help="Automatically attempts to connect to SERVER\nNote: This overrides autojoin settings in configuration files.")
    group.add_option("-c", "--channel", dest="channel", help="joins CHANNEL on SERVER if -s is specified, Otherwise, will attempt to join CHANNEL upon connecting to every server this session.\nNote: This overrides autojoin settings in the configuration files.")
    parser.add_option_group(group)

    (options, args) = parser.parse_args()
    return (options, args)

#-------------------------------------------------------------------------------
# Overwrite default settings and handle command-line stuff
#-------------------------------------------------------------------------------
(options, args) = ParseArgs()

if options.verbosity != None:
    if options.verbosity == True:
        Settings["Modes"]["OutputMode"] = "debug"
    elif options.verbosity == False:
        Settings["Modes"]["OutputMode"] = "quiet"
elif options.mode != None and options.mode.lower() in Settings["OutputModes"]:
    Settings["Modes"]["OutputMode"] = options.mode.lower()
else:
    Settings["Modes"]["OutputMode"] = "warning"
if options.server != None:
    for Network in Settings["IrcNetworks"]:
        Network["Autojoin"] = False
    Settings["IrcNetworks"][str(options.server)]["Autojoin"] = True
if options.channel != None:
    for GroupName, GroupInfo in Settings["IrcNetworks"].items():
        GroupInfo["AutojoinChannels"] = list()
        if options.server == None or options.server == GroupName:
            GroupInfo["AutojoinChannels"].append(str(options.channel))
del options
del args